package com.vshop.controller.login;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.vshop.entity.interfacerequest.ExceptionRequest;
import com.vshop.entity.interfacerequest.random.request.RequestRandom;
import com.vshop.entity.interfacerequest.random.request.RequestRandomData;
import com.vshop.entity.interfacerequest.random.response.ResponseRandom;
import com.vshop.entity.interfacerequest.random.response.ResponseRandomData;
import com.vshop.service.SequenceService;
import com.vshop.utils.Md5Encrypt;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 随便看看
 *
 * @author lm
 * @date 2019/03/07
 */
@RestController
@Api(description = "随便看看")
@Slf4j
public class RandomController {
    @Autowired
    private SequenceService sequenceService;

    @CrossOrigin
    @PostMapping(value = "random", produces = "application/json;charset=utf-8")
    @ResponseBody
    @ApiOperation(value = "随便看看", response = ResponseRandom.class)
    public String random(@ApiParam @RequestBody RequestRandom request) {
        // 1，生成流水号
        String connSeqNo = sequenceService.getPackageTaskNo();
        // 2,生成当前时间戳
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String curtime = df.format(new Date());

        //3,定义要回复的内容
        String jsonString;

        ExceptionRequest utils = new ExceptionRequest();
        String requestStr = JSON.toJSONString(request);
        if (requestStr == null) {
            log.info("请求报文不合法:requestStr为空");
            jsonString = utils.dataWeiKong(connSeqNo, curtime);
            return jsonString;
        } else {
            log.info("前端请求报文是：" + requestStr);
            RequestRandom requestPackage = JSONArray.parseObject(requestStr, RequestRandom.class);
            String requestSign = requestPackage.getSign();
            if (requestPackage.getSign() == null) {
                log.info("请求报文不合法:sign为空");
                jsonString = utils.dataWeiKong(connSeqNo, curtime);
                return jsonString;
            } else {
                log.info("请求的签名是：" + requestSign);
                RequestRandomData requestData = requestPackage.getData();
                if (requestPackage.getData() == null) {
                    log.info("请求报文不合法:data为空");
                    jsonString = utils.dataWeiKong(connSeqNo, curtime);
                    return jsonString;
                } else {
                    String ConnSeqNo = requestData.getConnSeqNo();
                    String partnerCode = requestData.getPartnerCode();
                    String tradeTime = requestData.getTradeTime();
                    String tradeType = requestData.getTradeType();
                    String lang = requestData.getLang();
                    String version = requestData.getVersion();

                    if (StringUtils.isBlank(ConnSeqNo) || StringUtils.isBlank(partnerCode) || StringUtils.isBlank(tradeTime) || StringUtils.isBlank(tradeType) || lang == null || version == null) {
                        log.info("请求报文不合法:data参数不合法");
                        jsonString = utils.dataWeiKong(connSeqNo, curtime);
                        return jsonString;
                    } else {
                        // 将请求体json转成String
                        String str = JSON.toJSONString(requestData);
                        // 将Str拿去计算签名
                        String sign = utils.calculateSignature(str);
                        log.info("计算的签名是：" + sign);
                        if (!requestSign.equalsIgnoreCase(sign)) {
                            log.info("签名验证失败！");
                            jsonString = utils.signatureFailure(ConnSeqNo, tradeTime, tradeType, "0001", "签名有误，请确认签名！");
                            return jsonString;
                        } else {//签名验证通过
                            if (StringUtils.isNotBlank(partnerCode)) {
                                ResponseRandomData data = new ResponseRandomData();
                                data.setConnSeqNo(ConnSeqNo);
                                data.setPartnerCode(partnerCode);
                                data.setTradeTime(curtime);
                                data.setTradeType(tradeType);
                                data.setDeviceCode("00000000000000000000");
                                data.setTradeRstCode("0000");

                                if (lang.equals("cn") || StringUtils.isBlank(lang)) {
                                    data.setTradeRstMessage("成功");
                                } else {
                                    data.setTradeRstMessage("success");
                                }
                                ResponseRandom response = new ResponseRandom();
                                response.setData(data);
                                response.setSign(Md5Encrypt.md5(JSON.toJSONString(data)));

                                jsonString = JSON.toJSONString(response);
                                log.info("响应报文：" + jsonString);
                                return jsonString;

                            } else {
                                ResponseRandomData data = new ResponseRandomData();
                                data.setTradeRstCode("0060");

                                if (lang.equals("cn") || StringUtils.isBlank(lang)) {

                                    data.setTradeRstMessage("partnerCode 不能为空");
                                } else {
                                    data.setTradeRstMessage("partnerCode cannot be empty");
                                }

                                ResponseRandom response = new ResponseRandom();
                                response.setData(data);
                                response.setSign(Md5Encrypt.md5(JSON.toJSONString(data)));

                                jsonString = JSON.toJSONString(response);
                                log.info("响应报文：" + jsonString);
                                return jsonString;


                            }


                        }

                    }
                }
            }
        }
    }
}